/** 创建组件对象 * */
var ReportForm = new Form();
var ReportTable = new Table();
var ReportPage = new Page();
var ReportDialog = new Dialog();
var ReportAjax = new Ajax();
var array = [ "ReportQueryDiv", "ReportButtonDiv", "ReportPageDiv" ];

/** 页面加载完执行 * */
$(function() {
	initScript();
	initConditions();
	initButtons();
	initPage();
	pageQuery();
	resetTableHeight("ReportTableDiv", array);
	ReportTable.reloadData();
	initElementsControl();
});

/** 初始化script * */
function initScript() {
	loadScript(appPath + "/project-yddemo/report/locale/Report_" + appLocale
			+ ".js"); // 1.加载国际化js*
	loadScript(appPath
			+ "/project-yddemo/report/properties/Report1Properties_View.js"); // 2.加载配置js(非必配,配置也可写在本js中)
	//loadScript(appPath + "/project-yddemo/common/Common.js");
}

/** 初始化查询条件 * */
function initConditions() {
	ReportForm.initTableConditions({
		divId : "ReportQueryDiv", // 1.归属div*
		type : "table",
		conditions : Report1Properties_View.getViewConditions(), // 2.配置条件*
		button : { // 4.操作按钮*
			width : "80", // 占用宽度*
			align : "center", // left,center,right,百分比*
			label : commonLocale.text.search, // 文本*
			action : "yddemo:report:report1",// 增加页面元素控制
			clickFun : function() { // 按钮事件*
				ReportTable.reloadData();
			}
		}
	});
}

/** 初始化按钮 * */
function initButtons() {
	ReportTable.initButtons({
		divId : "ReportButtonDiv", // 1.归属div*
		buttons : Report1Properties_View.getViewButtons()
	// 2.按钮组配置*
	});
	disableButton();
}

/** 初始化分页 * */
function initPage() {
	ReportPage.initPage({
		divId : "ReportPageDiv", // 1.归属div*
		eachPageNum : 20, // 2.每页条数(5,10,15,20)*
		pageChangeFun : function() { // 3.分页刷新事件*
			ReportTable.reloadData();
		}
	});
}

/** 分页查询 * */
function pageQuery() {
	ReportTable.initTable({
		divId : "ReportTableDiv", // 1.归属div*
		dataAjax : { // 2.发送ajax请求*
			url : requestPath + "/common/Reports", // 请求url*
			type : "GET", // 请求类型*
			data : ReportForm.getQueryConditions(), // 请求参数*
			page : ReportPage
		// 分页对象(需要分页时配置)
		},
		columns : Report1Properties_View.getViewColumns(),// 3.表格列配置
		rowClickFun : function(data) {
			if (sessionStorage.orgCode != "*") {
				btnStatus();
			}
		}
	});
	disableButton();
}

function btnStatus() {
	$("[action='common:Report:edit']").unbind();
	$("[action='common:Report:delete']").unbind();
	if (ReportTable.getSelectDatas().length > 0) {
		setElementsEnabled("common:Report:delete", "button", true);
		if (ReportTable.getSelectDatas().length != 1) {
			setElementsEnabled("common:Report:edit", "button", false);
		} else {
			setElementsEnabled("common:Report:edit", "button", true);
		}
	} else {
		disableButton();
	}
	$("[action='common:Report:edit']").bind("click", function() {
		updateReport();
	});
	$("[action='common:Report:delete']").bind("click", function() {
		deleteReport();
	});
}

function disableButton() {
	if (sessionStorage.orgCode == "*") {
		setElementsEnabled("common:Report:add", "button", false);
	}
	setElementsEnabled("common:Report:edit", "button", false);
	setElementsEnabled("common:Report:delete", "button", false);
}

/** 新增 * */
function addReport() {
	ReportDialog.iframeDialog(requestPath + "/common/Reports/page/add",
			commonLocale.text.add, 450, 340, "common:Report:add", false);
}

/** 修改 * */
function updateReport() {
	var selectDatas = ReportTable.getSelectDatas();
	if ($("[action='common:Report:edit']").hasClass("disabled")) {
		return false;
	}
	sessionStorage.selectedId = selectDatas[0].id;
	updateData();
}
function updateData() {
	ReportDialog.iframeDialog(requestPath + "/common/Reports/page/edit",
			commonLocale.text.edit, 450, 340, "common:Report:edit", false);
}

/** 删除 * */
function deleteReport() {
	if ($("[action='common:Report:delete']").hasClass("disabled")) {
		return false;
	}
	deleteData();
}
function deleteData() {
	ReportDialog.confirmDialog(commonLocale.text.isDelete, function() {
		var selectDatas = ReportTable.getSelectDatas();
		var dataList = [];
		for (var i = 0; i < selectDatas.length; i++) {
			dataList.push(selectDatas[i].Report.id);
		}
		var resultJson = ReportAjax.sendAjax({
			url : requestPath + "/common/Reports/", // 请求url*
			params : { // 请求url参数(非必配)
				_method : "DELETE"
			},
			async : false, // 是否异步*
			type : "POST", // 请求类型*
			data : dataList
		});
		if (resultJson.success) {
			ReportDialog.tipDialog(commonLocale.text.deleteSuccess,
					function() {
						// pageQuery(); // 删除成功后,刷新页面
						ReportTable.reloadData();
						disableButton();
					});
		} else {
			ReportDialog.alertDialog(resultJson.message); // 删除失败后,弹出警告框
		}
	});
}

/**
 * 窗口大小改变时触发
 *
 * @param
 * @return
 */
$(window).resize(function() {
	resetTableHeight("ReportTableDiv", array);
});
